/*
 * 文 件 名 : FriendDAO.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-9-18
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.fire8.dolphin.model.Friend;
import com.fire8.dolphin.model.UserInfo;

/**
 * 好友列表操作类
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-9-18]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class FriendDAO
{
    private LixgDBHelper dbHelper;
    
    public FriendDAO(Context context)
    {
        dbHelper = new LixgDBHelper(context);
    }
    public  void add(Friend friend)
    {
        if(isExist(friend.getAccount()))
        {
            return;
        }
        ContentValues values = new ContentValues();
        values.put(Friend.C_FRIEND_ID, friend.getFriendId());
        values.put(Friend.C_USER_ID, friend.getRemoteId());
        values.put(Friend.C_GROUP_ID, friend.getGroupId());
        values.put(Friend.C_ACCOUNT, friend.getAccount());
        values.put(Friend.C_USER_NAME, friend.getUserName());
        values.put(Friend.C_USER_HEAD, friend.getUserHead());
        values.put(Friend.C_REMARK, friend.getRemark());
        values.put(Friend.C_ON_LINE, friend.getOnline());
        values.put(Friend.C_HAS_MSG, 0);
        dbHelper.getWritableDatabase().insert(Friend.TABLE_NAME, null, values );
        dbHelper.close();
    }
    public  void add(UserInfo user)
    {
        if(isExist(user.getAccount()))
        {
            return;
        }
        ContentValues values = new ContentValues();
        //values.put(Friend.C_FRIEND_ID, friend.getFriendId());
        values.put(Friend.C_USER_ID, user.getUserId());
        values.put(Friend.C_ACCOUNT, user.getAccount());
        //values.put(Friend.C_GROUP_ID, friend.getGroupId());
        values.put(Friend.C_USER_NAME, user.getUserName());
        values.put(Friend.C_USER_HEAD, user.getHeadPic());
        values.put(Friend.C_REMARK, user.getRemark());
        values.put(Friend.C_HAS_MSG, 0);
        //values.put(Friend.C_ON_LINE, friend.getOnline());
        dbHelper.getWritableDatabase().insert(Friend.TABLE_NAME, null, values );
        dbHelper.close();
    }
    
    private boolean isExist(String account)
    {
        boolean exits=false;
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Friend.C_FRIEND_ID)
        .append(" FROM ")
        .append(Friend.TABLE_NAME);
        sql.append(" where ").append(Friend.C_ACCOUNT).append("=?");
        args.add(account);
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        if(cursor != null)
        {
            if(cursor.moveToNext())
            {
                exits = true;
            }
            cursor.close();
        }
        dbHelper.close();
        return exits;
    }
    
    public HashMap<String, String> getObject(String id)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Friend.C_FRIEND_ID)
        .append(",").append(Friend.C_USER_ID)
        .append(",").append(Friend.C_GROUP_ID)
        .append(",").append(Friend.C_ACCOUNT)
        .append(",").append(Friend.C_USER_NAME)
        .append(",").append(Friend.C_USER_HEAD)
        .append(",").append(Friend.C_REMARK)
        .append(",").append(Friend.C_ON_LINE)
        .append(",").append(Friend.C_HAS_MSG)
        .append(" FROM ")
        .append(Friend.TABLE_NAME)
        .append(" where 1=1 ");
        if(id != null)
        {
            sql.append(" and ").append(Friend.C_FRIEND_ID).append("=?");
            args.add(id);
        }
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        if(!list.isEmpty())
        {
            return list.get(0);
        }
        return null;
    }
    
    public List<HashMap<String, String>> findList(Friend friend)
    {
        List<String> args = new ArrayList<String>();
        StringBuffer sql = new StringBuffer("SELECT ");
        sql.append(Friend.C_FRIEND_ID)
        .append(",").append(Friend.C_USER_ID)
        .append(",").append(Friend.C_GROUP_ID)
        .append(",").append(Friend.C_ACCOUNT)
        .append(",").append(Friend.C_USER_NAME)
        .append(",").append(Friend.C_USER_HEAD)
        .append(",").append(Friend.C_REMARK)
        .append(",").append(Friend.C_ON_LINE)
        .append(",").append(Friend.C_HAS_MSG)
        .append(" FROM ")
        .append(Friend.TABLE_NAME)
        .append(" where 1=1 ");
        if(friend != null && friend.getFriendId() != null)
        {
            sql.append(" and ").append(Friend.C_FRIEND_ID).append("=?");
            args.add(friend.getFriendId().toString());
        }
        
        Cursor cursor = dbHelper.getReadableDatabase().rawQuery(sql.toString(), args.toArray(new String[]{}));
        List<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
        dbHelper.cursorToHashMap(cursor, list);
        if(cursor != null)
        {
            cursor.close();
            cursor = null;
        }
        dbHelper.close();
        return list;
    }
    
    /**
     * 修改好友是否有新消息
     * <功能详细描述>
     * @param account
     * @param flag 是否有新消息[0：没有；1：有]
     * @author lixg
     * @see [类、类#方法、类#成员]
     */
    public void hasMsg(String account,Integer flag)
    {
        List<String> args = new ArrayList<String>();
        args.add(account);
        ContentValues values = new ContentValues();
        values.put(Friend.C_HAS_MSG, flag);
        dbHelper.getReadableDatabase().update(Friend.TABLE_NAME, values, Friend.C_ACCOUNT+"=?", args.toArray(new String[]{}));
        dbHelper.close();
    }
}
